package com.cpfmat.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cpfmat.entity.Article;
import com.cpfmat.mapper.ArticleMapper;
import com.cpfmat.service.IArticleService;
import com.cpfmat.vo.CustomArticle;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangbin
 * @since 2018-07-30
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
	@Autowired
	private ArticleMapper articleMapper;
	
	@Override
	public Page<CustomArticle> getManagePage(String title, String columnId, String parentColumnId, int pageNum, int pageSize) {
		Map<String,Object> param = new HashMap<String,Object>();
		
		if(StringUtils.isNotBlank(title)) {
			param.put("title", title);
		}
		
		if(StringUtils.isNotBlank(columnId)){
			param.put("columnId", columnId);
		}
		
		if(StringUtils.isNotBlank(parentColumnId)){
			param.put("parentColumnId", parentColumnId);
		}
		
		Page<CustomArticle> resultPage = new Page<CustomArticle>(pageNum,pageSize);
		
		List<CustomArticle> resultList = null;
		resultList = articleMapper.getManagePage(resultPage,param);
		
		resultPage.setRecords(resultList);
		
		return resultPage;
	}

	@Override
	public Page<CustomArticle> getPage(String title, String columnId, String parentColumnId, int pageNum, int pageSize) {
		Map<String,Object> param = new HashMap<String,Object>();
		if(pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);
		
		if(StringUtils.isNotBlank(title)) {
			param.put("title", title);
		}
		
		if(StringUtils.isNotBlank(columnId)){
			param.put("columnId", columnId);
		}
		
		if(StringUtils.isNotBlank(parentColumnId)){
			param.put("parentColumnId", parentColumnId);
		}
		
		long counts = articleMapper.countList(param);
		
		List<CustomArticle> resultList = null;
		if(counts > 0) {
			resultList = articleMapper.getList(param);
		}
		
		Page<CustomArticle> resultPage = new Page<CustomArticle>(pageNum, pageSize);
		
		resultPage.setRecords(resultList);
		resultPage.setTotal(counts);
		
		return resultPage;
	}
	
	@Override
	public Page<CustomArticle> getRecommendPage(String columnId, String parentColumnId, int pageNum, int pageSize) {
		Map<String,Object> param = new HashMap<String,Object>();
		if(pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);
		
		if(StringUtils.isNotBlank(columnId)){
			param.put("columnId", columnId);
		}
		
		if(StringUtils.isNotBlank(parentColumnId)){
			param.put("parentColumnId", parentColumnId);
		}
		
		long counts = articleMapper.countList(param);
		
		List<CustomArticle> resultList = null;
		if(counts > 0) {
			resultList = articleMapper.getRecommendList(param);
		}
		
		Page<CustomArticle> resultPage = new Page<CustomArticle>();
		
		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);
		
		return resultPage;
	}

	@Override
	public CustomArticle detailWithAttachments(String seqId) {
		if(StringUtils.isNotBlank(seqId)){
			return articleMapper.queryArticleWithAttachments(seqId);
		}
		else{
			return null;
		}
	}

	@Override
	public int customUpdate(Article article) {
		return articleMapper.customUpdate(article);
	}

	@Override
	public Page<CustomArticle> getAllPage(String title, int pageNum, int pageSize) {
		Map<String,Object> param = new HashMap<String,Object>();
		if(pageNum > 0) {
			param.put("pageFrom", (pageNum - 1) * pageSize);
		}
		param.put("pageSize", pageSize);
		
		if(StringUtils.isNotBlank(title)) {
			param.put("title", title);
		}
		
		long counts = articleMapper.countAllList(param);
		
		List<CustomArticle> resultList = null;
		if(counts > 0) {
			resultList = articleMapper.getAllList(param);
		}
		
		Page<CustomArticle> resultPage = new Page<CustomArticle>();
		
		resultPage.setRecords(resultList);
		resultPage.setSize(pageSize);
		resultPage.setCurrent(pageNum);
		resultPage.setTotal(counts);
		
		return resultPage;
	}

}
